package com.blacamdk.ChatNetModule;
import com.blacamdk.ChatSecret.MD5Util;
import com.ck.GUI.ChatFrame;
import com.ck.GUI.Main;

import java.util.Random;
import java.util.Vector;

/**
 * ClientMain
 *
 * @author BlacAmDK
 * @date 16-12-15
 */
public class ClientMain {
    public static final int port = 14689;
    public static final String server = "blacamdk.com";
    public static int UID = -1;//UID
    public static String Fkey;//加密密钥(每次登录更新)
    public static long updateTime = 0L;//收取消息时的时间参数，默认为0
    public static Vector<User> userVector;
    static UDPClient client;
    public ClientMain()
    {
        userVector = new Vector<>();
        client = new UDPClient();
    }

    public static String login(String username, String password)
    {
        Fkey = MD5Util.MD5("" + new Random().nextLong());
        String[] ret = Utils.Analyze(client.sendData("20 " + username + " " + MD5Util.MD5(password) + " " + Fkey + " 1"));
        if(ret == null || (21 != Integer.parseInt(ret[0]) && 99 != Integer.parseInt(ret[0])))
            return Utils.getInf("OTHER");
        if(ret[1].equals("OK"))
            UID = Integer.parseInt(ret[2]);
        return Utils.getInf(ret[1]);
    }
    public static String register(String username, String password)
    {
        String[] ret = Utils.Analyze(client.sendData("15 " + username + " " + MD5Util.MD5(password)));
        if(ret == null || (16 != Integer.parseInt(ret[0]) && 99 != Integer.parseInt(ret[0])))
            return Utils.getInf("OTHER");
        return Utils.getInf(ret[1]);
    }
    public static void logout()
    {
        Utils.Analyze(client.sendData("18 " + Fkey + " " + UID));
    }
    public static long sendTo(int UIDTo, String Content)
    {
        String[] ret = Utils.Analyze(client.sendData("30 " + Fkey + " " + UID + " " + UIDTo + " " + Content));
        if(ret == null || (31 != Integer.parseInt(ret[0]) && 99 != Integer.parseInt(ret[0])))
            return -1;
        return Long.parseLong(ret[1]);
    }
    public static void getUserList()
    {
        String[] ret = Utils.Analyze(client.sendData("10 " + Fkey + " " + UID));
        if(ret == null || 11 != Integer.parseInt(ret[0]) || 99 == Integer.parseInt(ret[0]))
            return;
        userVector.removeAllElements();//清空好友列表重新获取
        for(int i = 1; i < ret.length && Integer.parseInt(ret[i]) > 0; i += 2)
        {
            userVector.add(new User(Integer.parseInt(ret[i]), ret[i + 1]));
        }
    }
    public static Vector<Message> recv()
    {
        String[] ret = Utils.Analyze(client.sendData("40 " + Fkey + " " + UID + " " + updateTime));
        if(ret == null || 41 != Integer.parseInt(ret[0]) || 99 == Integer.parseInt(ret[0]))
            return null;
        Vector<Message> retv = new Vector<>();
        Message m;
        for(int i = 1; i + 3 < ret.length && Integer.parseInt(ret[i]) > 0; i += 3)
        {
            m = new Message(Integer.parseInt(ret[i]), ret[i + 1], Long.parseLong(ret[i + 2]));
            if(m.getTime() > updateTime)    updateTime = m.getTime();//更新updateTime
            retv.add(m);
        }
        return retv;
    }

    public static int getIndexFromUsername(String Username)
    {
        for(int i = 0; i < userVector.size(); i++)
        {
            if(userVector.get(i).getUsername().equals(Username))
                return i;
        }
        return -1;
    }

    public static int getIndexFromUID(int UID)
    {
        for(int i = 0; i < userVector.size(); i++)
        {
            if(userVector.get(i).getUID() == UID)
                return i;
        }
        return -1;
    }

    //更新用户列表UI
    public static void updateUserList()
    {
        getUserList();
        Main.cf.onLineListModel.clear();
        for (User u:userVector
             ) {
            //UI添加一项用户5
            Main.cf.onLineListModel.addElement(u.getUsername());
        }
        Main.cf.onlineList.setModel(Main.cf.onLineListModel);

        //设置在线用户列表
        ChatFrame.onlineCountLbl.setText("在线用户列表(" + userVector.size() + ")");
        Main.cf.onlineList.repaint();
    }

    public static void main(String[] args) {
    }
}
